﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using OgilvyOne.IDAL;
using OgilvyOne.SqlServerDAL;
using OgilvyOne.Model;
namespace OgilvyOne.SQLServerDAL
{
    /// <summary>
    /// 数据访问类:BrandInfoDal
    /// </summary>
    public partial class BrandInfoDal : IBrandInfo
    {
        string connstr = SQLHelper.CONN_STRING_NON_DTC;
        public BrandInfoDal()
        { }
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string name, int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from COFCO_BRAND_INFO");
            strSql.Append(" where BRAND_NAME=@BRAND_NAME and BRAND_ID not in (" + id + ")");
            SqlParameter[] parameters = {
					new SqlParameter("@BRAND_NAME", SqlDbType.VarChar,100)
                    };
            parameters[0].Value = name;

            return int.Parse(SQLHelper.ExecuteScalar(connstr, CommandType.Text, strSql.ToString(), parameters).ToString()) > 0;
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(BrandInfoModel model, string typeIdlist)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(@"declare @num int;select @num=max(ORDER_NUM)from COFCO_BRAND_INFO where BRAND_TYPE_ID in (" + typeIdlist + ");");
            strSql.Append("insert into COFCO_BRAND_INFO(");
            strSql.Append("BRAND_NAME,BRAND_IMG,BRAND_DESC,BRAND_TYPE_ID,ORDER_NUM,STATE,ADD_TIME,BUY_LINK,BRAND_VIDEO_FILE,ADDUSERNAME,BRAND_LOGO,IS_INDEX)");
            strSql.Append(" values (");
            strSql.Append("@BRAND_NAME,@BRAND_IMG,@BRAND_DESC,@BRAND_TYPE_ID,case when @num is null then 1 else @num+1 end ,@STATE,@ADD_TIME,@BUY_LINK,@BRAND_VIDEO_FILE,@ADDUSERNAME,@BRAND_LOGO,@IS_INDEX)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@BRAND_NAME", SqlDbType.VarChar,100),
					new SqlParameter("@BRAND_IMG", SqlDbType.VarChar,50),
					new SqlParameter("@BRAND_DESC", SqlDbType.Text),
					new SqlParameter("@BRAND_TYPE_ID", SqlDbType.Int,4),					
					new SqlParameter("@STATE", SqlDbType.Int,4),
					new SqlParameter("@ADD_TIME", SqlDbType.DateTime),
                  new SqlParameter("@BUY_LINK", SqlDbType.VarChar,50),
                  new SqlParameter("@BRAND_VIDEO_FILE", SqlDbType.VarChar,50),
					new SqlParameter("@ADDUSERNAME", SqlDbType.VarChar,50),
                  new SqlParameter("@BRAND_LOGO", SqlDbType.VarChar,50),
                  new SqlParameter("@IS_INDEX", SqlDbType.Int,4)
        };
            parameters[0].Value = model.Brandname;
            parameters[1].Value = model.Brandimg;
            if (model.Branddesc == null)
            {
                parameters[2].Value = DBNull.Value;
            }
            else
               parameters[2].Value = model.Branddesc;
            parameters[3].Value = model.Brandtypeid;
            parameters[4].Value = model.State;
            parameters[5].Value = model.Addtime;           
            if (model.Buylink == null)
            {
                parameters[6].Value = DBNull.Value;
            }
            else
                parameters[6].Value = model.Buylink;
            if (model.Brand_video_file == null)
            {
                parameters[7].Value = DBNull.Value;
            }
            else
                parameters[7].Value = model.Brand_video_file;
            parameters[8].Value = model.Addusername;
            if (model.Brandlogo == null)
            {
                parameters[9].Value = DBNull.Value;
            }
            else
            parameters[9].Value = model.Brandlogo;
            parameters[10].Value = model.IsIndex;
            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters);

        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(BrandInfoModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update COFCO_BRAND_INFO set ");
            strSql.Append("BRAND_NAME=@BRAND_NAME,");
            strSql.Append("BRAND_IMG=@BRAND_IMG,");
            strSql.Append("BRAND_DESC=@BRAND_DESC,");
            strSql.Append("BRAND_TYPE_ID=@BRAND_TYPE_ID,");
            strSql.Append("ORDER_NUM=@ORDER_NUM,");
            strSql.Append("STATE=@STATE,");
            strSql.Append("ADD_TIME=@ADD_TIME,");
            strSql.Append("BUY_LINK=@BUY_LINK,");
            strSql.Append("BRAND_VIDEO_FILE=@BRAND_VIDEO_FILE,");
            strSql.Append("ADDUSERNAME=@ADDUSERNAME,");
            strSql.Append("BRAND_LOGO=@BRAND_LOGO,");
            strSql.Append("IS_INDEX=@IS_INDEX");
            strSql.Append(" where BRAND_ID=@BRAND_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@BRAND_NAME", SqlDbType.VarChar,100),
					new SqlParameter("@BRAND_IMG", SqlDbType.VarChar,50),
					new SqlParameter("@BRAND_DESC", SqlDbType.Text),
					new SqlParameter("@BRAND_TYPE_ID", SqlDbType.Int,4),
					new SqlParameter("@ORDER_NUM", SqlDbType.Int,4),
					new SqlParameter("@STATE", SqlDbType.Int,4),
					new SqlParameter("@ADD_TIME", SqlDbType.DateTime),
                    new SqlParameter("@BUY_LINK", SqlDbType.VarChar,50),
                    new SqlParameter("@BRAND_VIDEO_FILE", SqlDbType.VarChar,50),
                    
					new SqlParameter("@ADDUSERNAME", SqlDbType.VarChar,50),
                     new SqlParameter("@BRAND_LOGO", SqlDbType.VarChar,50),
                      new SqlParameter("@IS_INDEX", SqlDbType.Int,4),
					new SqlParameter("@BRAND_ID", SqlDbType.Int,4)};
            parameters[0].Value = model.Brandname;
            parameters[1].Value = model.Brandimg;
            if (model.Branddesc == null)
            {
                parameters[2].Value = DBNull.Value;
            }
            else
                parameters[2].Value = model.Branddesc;           
            parameters[3].Value = model.Brandtypeid;
            if (model.Ordernum == null)
            {
                parameters[4].Value = 1;
            }
            else
               parameters[4].Value = model.Ordernum;
            parameters[5].Value = model.State;
            if (model.Addtime == null)
            {
                parameters[6].Value = DBNull.Value;
            }
            else
                parameters[6].Value = model.Addtime;
            
            if (model.Buylink == null)
            {
                parameters[7].Value = DBNull.Value;
            }
            else
                parameters[7].Value = model.Buylink;
            if (model.Brand_video_file == null)
            {
                parameters[8].Value = DBNull.Value;
            }
            else
                parameters[8].Value = model.Brand_video_file;
            parameters[9].Value = model.Addusername;
            if (model.Brandlogo == null)
            {
                parameters[10].Value = DBNull.Value;
            }
            else
                parameters[10].Value = model.Brandlogo;


            parameters[11].Value = model.IsIndex;
            parameters[12].Value = model.Brandid;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters) > 0;
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int BRAND_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from COFCO_BRAND_INFO ");
            strSql.Append(" where BRAND_ID=@BRAND_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@BRAND_ID", SqlDbType.Int,4)
};
            parameters[0].Value = BRAND_ID;

            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), parameters) > 0;
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string BRAND_IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from COFCO_BRAND_INFO ");
            strSql.Append(" where BRAND_ID in (" + BRAND_IDlist + ")  ");
            return SQLHelper.ExecuteNonQuery(connstr, CommandType.Text, strSql.ToString(), null) > 0;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BrandInfoModel GetModel(int BRAND_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 BRAND_ID,BRAND_NAME,BRAND_IMG,BRAND_DESC,BRAND_TYPE_ID,ORDER_NUM,STATE,ADD_TIME,ADDUSERNAME,BUY_LINK,BRAND_VIDEO_FILE,BRAND_LOGO,IS_INDEX from COFCO_BRAND_INFO ");
            strSql.Append(" where BRAND_ID=" + BRAND_ID);
            BrandInfoModel model = new BrandInfoModel();
            DataSet ds = SQLHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["BRAND_ID"].ToString() != "")
                {
                    model.Brandid = int.Parse(ds.Tables[0].Rows[0]["BRAND_ID"].ToString());
                }
                model.Brandname = ds.Tables[0].Rows[0]["BRAND_NAME"].ToString();
                model.Brandimg = ds.Tables[0].Rows[0]["BRAND_IMG"].ToString();
                model.Branddesc = ds.Tables[0].Rows[0]["BRAND_DESC"].ToString();
                if (ds.Tables[0].Rows[0]["BRAND_TYPE_ID"].ToString() != "")
                {
                    model.Brandtypeid = int.Parse(ds.Tables[0].Rows[0]["BRAND_TYPE_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ORDER_NUM"].ToString() != "")
                {
                    model.Ordernum = int.Parse(ds.Tables[0].Rows[0]["ORDER_NUM"].ToString());
                }
                if (ds.Tables[0].Rows[0]["STATE"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["STATE"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ADD_TIME"].ToString() != "")
                {
                    model.Addtime = DateTime.Parse(ds.Tables[0].Rows[0]["ADD_TIME"].ToString());
                }
                model.Addusername = ds.Tables[0].Rows[0]["ADDUSERNAME"].ToString();
                if (ds.Tables[0].Rows[0]["BUY_LINK"].ToString() != "")
                {
                    model.Buylink = ds.Tables[0].Rows[0]["BUY_LINK"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BRAND_VIDEO_FILE"].ToString() != "")
                {
                    model.Brand_video_file = ds.Tables[0].Rows[0]["BRAND_VIDEO_FILE"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BRAND_LOGO"].ToString() != "")
                {
                    model.Brandlogo = ds.Tables[0].Rows[0]["BRAND_LOGO"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IS_INDEX"].ToString() != "")
                {
                    model.IsIndex = int.Parse(ds.Tables[0].Rows[0]["IS_INDEX"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public BrandInfoModel GetModelByOrderNum(int orderNum, string idlist)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 BRAND_ID,BRAND_NAME,BRAND_IMG,BRAND_DESC,BRAND_TYPE_ID,ORDER_NUM,STATE,ADD_TIME,ADDUSERNAME,BUY_LINK,BRAND_VIDEO_FILE,BRAND_LOGO,IS_INDEX from COFCO_BRAND_INFO ");
            strSql.Append(" where ORDER_NUM=" + orderNum + "and BRAND_TYPE_ID in (" + idlist + ")");
            BrandInfoModel model = new BrandInfoModel();
            DataSet ds = SQLHelper.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["BRAND_ID"].ToString() != "")
                {
                    model.Brandid = int.Parse(ds.Tables[0].Rows[0]["BRAND_ID"].ToString());
                }
                model.Brandname = ds.Tables[0].Rows[0]["BRAND_NAME"].ToString();
                model.Brandimg = ds.Tables[0].Rows[0]["BRAND_IMG"].ToString();
                model.Branddesc = ds.Tables[0].Rows[0]["BRAND_DESC"].ToString();
                if (ds.Tables[0].Rows[0]["BRAND_TYPE_ID"].ToString() != "")
                {
                    model.Brandtypeid = int.Parse(ds.Tables[0].Rows[0]["BRAND_TYPE_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ORDER_NUM"].ToString() != "")
                {
                    model.Ordernum = int.Parse(ds.Tables[0].Rows[0]["ORDER_NUM"].ToString());
                }
                if (ds.Tables[0].Rows[0]["STATE"].ToString() != "")
                {
                    model.State = int.Parse(ds.Tables[0].Rows[0]["STATE"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ADD_TIME"].ToString() != "")
                {
                    model.Addtime = DateTime.Parse(ds.Tables[0].Rows[0]["ADD_TIME"].ToString());
                }
                model.Addusername = ds.Tables[0].Rows[0]["ADDUSERNAME"].ToString();
                if (ds.Tables[0].Rows[0]["BUY_LINK"].ToString() != "")
                {
                    model.Buylink = ds.Tables[0].Rows[0]["BUY_LINK"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BRAND_VIDEO_FILE"].ToString() != "")
                {
                    model.Brand_video_file = ds.Tables[0].Rows[0]["BRAND_VIDEO_FILE"].ToString();
                }
                if (ds.Tables[0].Rows[0]["BRAND_LOGO"].ToString() != "")
                {
                    model.Brandlogo = ds.Tables[0].Rows[0]["BRAND_LOGO"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IS_INDEX"].ToString() != "")
                {
                    model.IsIndex = int.Parse(ds.Tables[0].Rows[0]["IS_INDEX"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select BRAND_ID,BRAND_NAME,BRAND_IMG,BRAND_DESC,BRAND_TYPE_ID,ORDER_NUM,STATE,ADD_TIME,ADDUSERNAME,BUY_LINK,BRAND_VIDEO_FILE,BRAND_LOGO,IS_INDEX ");
            strSql.Append(" FROM COFCO_BRAND_INFO ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by  ORDER_NUM");
            return SQLHelper.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" BRAND_ID,BRAND_NAME,BRAND_IMG,BRAND_DESC,BRAND_TYPE_ID,ORDER_NUM,STATE,ADD_TIME,ADDUSERNAME,BUY_LINK,BRAND_VIDEO_FILE,BRAND_LOGO,IS_INDEX ");
            strSql.Append(" FROM COFCO_BRAND_INFO ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLHelper.Query(strSql.ToString());
        }
                 
        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "COFCO_BRAND_INFO";
            parameters[1].Value = "BRAND_ID";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        public int getNum(string brandTypeId)
        {
            string strSql = "select count(*)from [COFCO_BRAND_INFO] where BRAND_TYPE_ID=" + brandTypeId;
            return int.Parse(SQLHelper.ExecuteScalar(connstr, CommandType.Text, strSql.ToString(), null).ToString());
        }
        #endregion  Method
    }
}

